"""
    ********************* VerySimpleWebBrowser ************************

    This is a Very Simple Web Browser implemented over Qt and QtWebKit.

    author: Juan Manuel Garcia <jmg.utn@gmail.com>

    *******************************************************************
"""

import sys
from PyQt4 import QtCore, QtGui, QtWebKit

class Browser(QtGui.QMainWindow):
    
    def __init__(self):
        """
            Initialize the browser GUI and connect the events
        """

        QtGui.QMainWindow.__init__(self)
        self.resize(800,600)
        self.centralwidget = QtGui.QWidget(self)
        self.mainLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.mainLayout.setSpacing(0)
        self.mainLayout.setMargin(1)

        self.frame = QtGui.QFrame(self.centralwidget)
        self.gridLayout = QtGui.QVBoxLayout(self.frame)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)

        self.horizontalLayout = QtGui.QVBoxLayout()
        self.tb_url = QtGui.QLineEdit(self.frame)
        self.bt_back = QtGui.QPushButton(self.frame)
        self.bt_ahead = QtGui.QPushButton(self.frame)

        self.bt_back.setIcon(QtGui.QIcon().fromTheme("go-previous"))
        self.bt_ahead.setIcon(QtGui.QIcon().fromTheme("go-next"))

        self.horizontalLayout.addWidget(self.bt_back)
        self.horizontalLayout.addWidget(self.bt_ahead)
        self.horizontalLayout.addWidget(self.tb_url)
        self.gridLayout.addLayout(self.horizontalLayout)

        self.webview = QtWebKit.QWebView()
        self.gridLayout.addWidget(self.webview)
        self.mainLayout.addWidget(self.frame)
        self.setCentralWidget(self.centralwidget)
#        self.mainLayout.addWidget(self.webview)
        self.connect(self.tb_url, QtCore.SIGNAL("returnPressed()"), self.browse)
        self.connect(self.bt_back, QtCore.SIGNAL("clicked()"), self.webview.back)
        self.connect(self.bt_ahead, QtCore.SIGNAL("clicked()"), self.webview.forward)
#        self.connect(self.webview,QtCore.SIGNAL("loadFinish(bool)"),self.onLoadFinish)
        self.webview.loadFinished.connect(self.onLoadFinish)
        
#        self.default_url = "http://codescience.wordpress.com/"?
        self.default_url="./epub/14838/@public@vhost@g@gutenberg@html@files@14838@14838-h@14838-h-0.htm.html"
        self.tb_url.setText(self.default_url)
        
        
        self.frame = self.webview.page().mainFrame()
      
          
#        self.frame.evaluateJavaScript("alert('hello')")
        
        
        self.browse()
    def onLoadFinish(self):
        self.doc = self.frame.documentElement()
#        header = self.doc.findFirst("head")
#        style = header.createElement('style')
#        css="body{-webkit-column-count:1;-webkit-column-width:200px}"
#        self.frame.evaluateJavaScript(css)
#        style.appendChild(self.doc.createTextNode(css))
#        self.frame.evaluateJavaScript(style)
#         script= "var head = document.getElementsByTagName('head')[0]; var style = document.createElement('style'); var rules = document.createTextNode( "
#                "                                        '* {margin-top:0px; margin-bottom:0px; -webkit-nbsp-mode:space;}'+" +
#                "                                        'p {margin-left:0px !important; margin-right:0px !important;}'+" +
#                "                                        'img {max-width:"+columnWidth+"px !important; max-height:"+(columnHeight)+"px !important;}'+" +
#                "                                        'video {max-width:"+columnWidth+"px !important; max-height:"+(columnHeight)+"px !important;}'+" +
#                "                                        'body {-webkit-column-width:"+columnWidth+"px; -moz-column-width:"+columnWidth+"px; column-width:"+columnWidth+"px; column-fill:balance; -webkit-column-gap:0px; height:"+columnHeight+"px; width:"+columnWidth+"px;}'+" +
#                "                                        'body {margin:0px; padding-left:0px;  padding-right:0px; line-height:160%; text-align:justify;}'+ " +
#                "                                    ''); "+    
#                "style.type = 'text/css'; "+
#                "if(style.styleSheet) "+
#                "    style.styleSheet.cssText = rules.nodeValue; "+
#                "else" +
#                "    style.appendChild(rules); "+
#                "head.appendChild(style); " 
#                ;
#                loadUrl("javascript:(function() { "+function+" })()");

#        print header

        viewwidth=self.gridLayout.layout()
        paramWidth=self.webview.width()
        paramHeight=self.webview.height()
        
        script=""
        script+="var head=document.getElementsByTagName('head')[0];"
        script+="var style = document.createElement('style');"
        script+=" var rules = document.createTextNode( "
#        script+=" '* {margin-top:0px; margin-bottom:0px; -webkit-nbsp-mode:space;}'" 
#        script+=" 'p {margin-left:0px !important; margin-right:0px !important;}'" 
#        script+="  img {max-width:"+columnWidth+"px !important; max-height:"+(columnHeight)+"px !important;}'+" 
#        script+="                                        'video {max-width:"+columnWidth+"px !important; max-height:"+(columnHeight)+"px !important;}'+" +
        script+="    'body {-webkit-column-width:%dpx; -moz-column-width:%dpx; column-width:%dpx; column-fill:balance; -webkit-column-gap:10px; height:%dpx; width:%dpx;}'" % (paramWidth,paramWidth,paramWidth,paramHeight,paramWidth)
#        script+="    'body {margin:0px; padding-left:0px;  padding-right:0px; line-height:160%; text-align:justify;}'+ " 
        script+="    ); "    
        script+="style.type = 'text/css'; "
        script+="if(style.styleSheet) "
        script+="    style.styleSheet.cssText = rules.nodeValue; "
        script+="else" 
        script+="    style.appendChild(rules); "
        script+="head.appendChild(style); " 
 
        print script
        self.frame.evaluateJavaScript("javascript:function(){"+script+"}()")
        self.frame.evaluateJavaScript(script)

        print "call onloadfinish"
#        QtGui.QMessageBox.information(None, "Info", "message on load")
#        QtGui.QMessageBox.text("wonsork")
        

    def browse(self):
        """
            Make a web browse on a specific url and show the page on the
            Webview widget.
        """

        url = self.tb_url.text() if self.tb_url.text() else self.default_url
        self.webview.load(QtCore.QUrl(url))
        self.webview.show()
        self.messageWebViewInfo()
        
#    webview infomation
    def messageWebViewInfo(self):
        print "webView x =",self.webview.x()
        print "webView y =",self.webview.y()
        print "webView width-=",self.webview.width()
        print "webView height-=",self.webview.height()

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    main = Browser()
    main.show()
    sys.exit(app.exec_())
    
    
    